package com.tairanchina.md.meeting.service.impl;

import com.tairanchina.md.meeting.dao.YearMeetingSungoodsMapperExt;
import com.tairanchina.md.meeting.dto.SunGoodDto;
import com.tairanchina.md.meeting.entity.YearMeetingSunawarder;
import com.tairanchina.md.meeting.entity.YearMeetingSungoods;
import com.tairanchina.md.meeting.entity.YearMeetingSungoodsExample;
import com.tairanchina.md.meeting.service.YearMeetingSunGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lxliuxuan on 2016/12/24.
 */
@Transactional
@Service
public class YearMeetingSunGoodsServiceImpl implements YearMeetingSunGoodsService {

    @Resource
    private YearMeetingSungoodsMapperExt sunGoodsDao;

    @Override
    public SunGoodDto findAllSunGoods() {
        SunGoodDto sunGoodDto = new SunGoodDto();
        YearMeetingSungoodsExample example = new YearMeetingSungoodsExample();
        example.setOrderByClause("goods_level desc");
        List<YearMeetingSungoods> yearMeetingSungoodses = sunGoodsDao.selectByExample(example);
        sunGoodDto.setList(yearMeetingSungoodses);
        sunGoodDto.setSunGoodsNum(yearMeetingSungoodses.size());
        return sunGoodDto;
    }

    @Override
    public void updateSunGood(YearMeetingSunawarder awarderMsg) {
        YearMeetingSungoodsExample example = new YearMeetingSungoodsExample();
        YearMeetingSungoodsExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsMoneyEqualTo(awarderMsg.getAwardMoney());
        List<YearMeetingSungoods> sungoodses = sunGoodsDao.selectByExample(example);
        YearMeetingSungoods yearMeetingSungoods = null;
        if(sungoodses != null && !sungoodses.isEmpty() ){
            yearMeetingSungoods = sungoodses.get(0);
        }
        if(yearMeetingSungoods != null){
            Integer goodsNum = yearMeetingSungoods.getGoodsNum();
            if(goodsNum  >0){
                yearMeetingSungoods.setGoodsNum(goodsNum-1);
                sunGoodsDao.updateByPrimaryKeySelective(yearMeetingSungoods);
            }
        }
    }


}
